Module Simple Triggers

Tuple Documentation of module_simple_triggers.py
Each simple trigger contains the following fields:
1) Check interval: How frequently this trigger will be checked
2) Operation block: This must be a valid operation block.

There are two types of triggers at the M&B game engine: Regular triggers and simple triggers. We will begin with module_simple_triggers.py and thus the simple triggers because they are generally smaller and easier to understand.[1]

Before that though, what is a trigger? A trigger is a block of code that executes multiple times as the game progresses. It's called a trigger, because it fires - that is how it's called when the time comes for the trigger to do its job.

Simple triggers are the alternative to old style triggers. They do not preserve their state and are thus simpler to maintain. Simple triggers are generally found in module_simple_triggers.py and despite being simpler, they are not to be underestimated. Let's have a look at a simple trigger:

								
									(24, [
  (troop_add_gold, "trp_player", 100),
  ]),

This is rather simple, isn't it? Every 24 hours, so every day at the world map, this trigger will give 100 gold to the player. Yes, it makes no sense, but it works. And that is basically how every simple trigger operates. Here is the structure: (check_interval, [operations_block]),

This is shared by each and every simple trigger, including the gold-giving one which we have got above. Now let's look at the components: The check interval is how frequently this trigger will be checked, measured in in-game hours. Using 24 will make the trigger fire once every day, using 1 will make it fire 24 times per day. Using 0 as a check interval will make the trigger fire once every frame, which can be many times per second, dependending on the user's frame rate. Such triggers are potential performance hogs, so be cautious when using them.

The operations block is what happens when the trigger fires, and can contain whatever codes you want to place in it. Yes, checks are allowed. For an example, we can enhance our aforementioned code by doing this:

								
									(24, [
  # (try_begin), # We don't need try blocks right now
  (lt, "$player_honor", 0),
  (troop_add_gold, "trp_player", 100),
  # (try_end),
  ]),

Now it will give a hundred gold per day to the player, if he/she is dishonourable. It makes even less sense now, but whatever. As you can see, a trigger can fail with no problems whatsoever, however be careful when not wrapping statements in try_ blocks. Of course, it's always safer to use the try blocks, just in case.

								
									(24, [
  (try_begin), # Better safe than sorry
      (lt, "$player_honor", 0), # Make sure your indentation is always correct!
      (troop_add_gold, "trp_player", 100),
  (try_end),
  ]),

Right, so we've got the basics covered now. There are more things you need to know though. Simple triggers are also used in other places, such as on items and scene props, but with special conditions in form of hard-coded conditions passed by the engine at the right times, like for example ti_on_weapon_attack or ti_on_init_scene_prop. A list of all such special check intervals can be found in header_triggers.py. They will all get explained at their respective sections at which they can get used.

This sums up pretty much everything related to simple triggers, so you can move on to the next chapter about triggers.

Save-game Compatibility

save-game compatibility, cmpxchg8b, Modding Q&A

Deprecated Hardcoded Triggers

ti_on_party_encounter and ti_simulate_battle fully deprecated or still useable (given it makes sense to use them)? Two game scripts get active at the trigger moment anyway.

switch to world map trigger perhaps simple trigger specific, kalarhan, Modding Q&A

  • Remarks and Sources:
  • [1] The upfollowing paragraphs are taken from Lumos, Modding Q&A.